﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MyBind;
using DAL;

public partial class Communicate_BBSAddCata : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            //如果是修改信息则将原信息显示到控件中,保存至viewsate
            if (Request.QueryString["id"]!=null)
            {
                string id=Request.QueryString["id"];
                DAL.bbsCatalog dal = new DAL.bbsCatalog();
                Model.bbsCatalog obj=dal.GetModel(int.Parse(id));
                this.txtName.Text = obj.name;
                txtDepict.Text = obj.depict;
                ViewState["id"] =id;
                ViewState["originallyName"] = obj.name;
            }
        }
    }

    //添加/更新
    protected void imbSave_Click(object sender, ImageClickEventArgs e)
    {
        DAL.bbsCatalog dal=new bbsCatalog();
        string currentName = this.txtName.Text.Trim();
        string depict=this.txtDepict.Text.Trim();
        Model.bbsCatalog obj = new Model.bbsCatalog();
        if (currentName.Equals(""))
        {
            this.lblMsg.Text = "类名不能为空！";
        }
        //判断是进行添加还是更新
        else if(ViewState["id"]!=null)
        {
            int id=int.Parse(ViewState["id"].ToString());
            string originallyName = ViewState["originallyName"].ToString();
            //名称不存在或者未改变则进行添加
            if (!dal.Exists(currentName) || currentName.Equals(originallyName))
            {
                obj.id = id;
                obj.name = currentName;
                obj.depict = depict;
                //进行修改
                if (dal.Update(obj) > 0)
                {
                    WriteScript.Mesg(this.Response, "修改成功！");
                    if (Request.QueryString["from"] != null)
                    {
                        string url = Request.QueryString["from"].ToString();
                        WriteScript.ChangeLoca(this.Response, url+".aspx");
                    }
                    else
                    {
                        WriteScript.ChangeLoca(this.Response, "BBSCatList.aspx");
                    }
                }
                else
                {
                    WriteScript.Mesg(this.Response, "修改失败！");
                }
            }
            else
            {
                this.lblMsg.Text = "已存在同名的类别，请重新指定名称！";
            }
        }
        else//添加
        {
            if (dal.Exists(currentName))
            {
                this.lblMsg.Text = "已存在同名的类别，请重新指定名称！";
            }
            else
            {
                obj.name = currentName;
                obj.depict = depict;
                //进行添加
                if (dal.Add(obj) > 0)
                {
                    WriteScript.Mesg(this.Response, "添加成功！");
                    if (Request.QueryString["from"] != null)
                    {
                        string url = Request.QueryString["from"].ToString();
                        WriteScript.ChangeLoca(this.Response, url + ".aspx");
                    }
                    else
                    {
                        WriteScript.ChangeLoca(this.Response, "BBSCatList.aspx");
                    }
                }
                else
                {
                    WriteScript.Mesg(this.Response, "添加失败！");
                }
            }
        }
    }

    //返回
    protected void imbBack_Click(object sender, ImageClickEventArgs e)
    {
        if (Request.QueryString["from"] != null)
        {
            string url=Request.QueryString["from"].ToString();
            Response.Redirect(url+".aspx");
        }
        else
        {
            Response.Redirect("BBSCatList.aspx");
        }
    }
}
